package com.mc.controller;

import com.mc.annotation.Log;
import com.mc.interreceptor.ResponseResult;
import com.mc.mapper.model.User;
import com.mc.redis.RedisService;
import com.mc.result.CodeMsg;
import com.mc.result.Result;
import com.mc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//不能用@RestController rest默认返回json
@Controller
public class DemoController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private IUserService userService;


    @RequestMapping("/helloSuccess")
    public Result helloSuccess() {
        return Result.success("hello");
    }

    @RequestMapping("/helloError")
    public Result helloError() {
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @RequestMapping("/hello/thymeleaf")
    public String thymeleaf(Model model) {
        model.addAttribute("name", "Joshua");
        return "hello";
    }

    @RequestMapping("/db/get")
    @ResponseBody
    public Result dbGet() {

        return Result.success(userService.query(1));
    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public Result<Long> redisGet() {
        Long v1 = redisService.get("key1", Long.class);
        System.out.println(v1);
        return Result.success(v1);
    }

    @RequestMapping("/redis/set")
    @ResponseBody
    public Result<String> redisSet() {
        redisService.set("key3", "hello mc");
        User query = userService.query(1);
        String v3 = redisService.get("key3", String.class);
        System.out.println(v3);
        return Result.success(v3);
    }


    @RequestMapping("/log")
    @ResponseBody
    @Log(value = "testSaveLog")
    public Result<String> testLog(String name) {
        System.out.println(name);
        return Result.success(name);
    }

    @GetMapping("/testResponseResult")
    @ResponseResult
    public User testResponseResult(String name) {
        System.out.println(name);
        return new User();
    }


}